//
//  HYAudioPlayerProgressView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/22.
//

#import "HYAudioPlayerProgressView.h"
#import <HYUIKit/HYAppleLoadingView.h>
#import "HYVideoRoomViewController.h"

@interface HYAudioPlayerProgressView ()

@property (nonatomic, strong) UILabel *leftTimeLable;
@property (nonatomic, strong) UILabel *rightTimeLable;
@property (nonatomic, strong) HYAppleLoadingView *loadingView;
//加载状态
@property (nonatomic, assign) BOOL loading;

@end

@implementation HYAudioPlayerProgressView

- (void)dealloc 
{
    CLog(@"dealloc -- %@",self.class);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat gap = 68;
        CGFloat llrWidth = gap*(kIsIPad?GAP_SCALE:UI_SCALE);
        UILabel *leftTimeLable = [UILabel new];
        [self addSubview:leftTimeLable];
        leftTimeLable.textColor = APPTextThemeColor;
        leftTimeLable.font = [UIFont systemFontOfSize:SMALL_FONT_SIZE];
        [leftTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_left).mas_equalTo(llrWidth);
            make.centerY.mas_equalTo(0);
        }];
        leftTimeLable.textAlignment = NSTextAlignmentRight;
        leftTimeLable.text = @"00:00";
        self.leftTimeLable = leftTimeLable;
        
        UILabel *rightTimeLable = [UILabel new];
        [self addSubview:rightTimeLable];
        rightTimeLable.textColor = APPTextThemeColor;
        rightTimeLable.font = [UIFont systemFontOfSize:SMALL_FONT_SIZE];
        [rightTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_right).mas_equalTo(-llrWidth);
            make.centerY.mas_equalTo(leftTimeLable.mas_centerY);
        }];
        rightTimeLable.textAlignment = NSTextAlignmentLeft;
        rightTimeLable.text = @"00:00";
        self.rightTimeLable = rightTimeLable;
        
        HYVideoPlayerProressSlider *slider = [HYVideoPlayerProressSlider new];
        [self addSubview:slider];
        [slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(leftTimeLable.mas_right);
            make.right.mas_equalTo(rightTimeLable.mas_left);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(leftTimeLable.mas_centerY);
        }];
        slider.progressView.progressTintColor = [CommUtls colorWithHexString:@"#e1e1e1"];
        slider.progressView.trackTintColor = [CommUtls colorWithHexString:@"#f1f1f1"];
        self.slider = slider;
        
        UIView *thumbView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
        thumbView.backgroundColor = APPMainThemeColor;
        thumbView.layer.cornerRadius = 6;
        UIGraphicsBeginImageContextWithOptions(thumbView.frame.size,NO,0);
        [thumbView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *thumbimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        slider.slider.thumbImage = thumbimage;
        
        UIView *loadingSuperView = [UIView new];
        [self addSubview:loadingSuperView];
        [loadingSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(slider.slider.imageView);
            make.width.height.mas_equalTo(12);
        }];
        loadingSuperView.userInteractionEnabled = NO;
        
        HYAppleLoadingView *loadingView = [[HYAppleLoadingView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
        [loadingSuperView addSubview:loadingView];
        [loadingView startAnimating];
        self.loadingView = loadingView;
    }
    return self;
}

/**
 设置显示时间
 
 @param time <#time description#>
 */
- (void)setShowTime:(NSString *)time 
{
    NSArray *array = [time componentsSeparatedByString:@"/"];
    self.leftTimeLable.text = array.firstObject?:@"00:00";
    self.rightTimeLable.text = array.lastObject?:@"00:00";
}

- (void)setLoading:(BOOL)loading 
{
    if (loading) {
        [self.loadingView startAnimating];
    } else {
        [self.loadingView stopAnimating];
    }
}

- (void)bindRoomPlayerSignal:(HYVideoRoomViewController *)player
{
    RACChannelTerminal *channelA = RACChannelTo(player.bottomToolBar.slider.slider, progress);
    RACChannelTerminal *channelB = RACChannelTo(self.slider.slider, progress);
    [channelA subscribe:channelB];
    [channelB subscribe:channelA];
    
    RAC(self, leftTimeLable.text) = RACObserve(player.bottomToolBar, currentTimeLabel.text);
    RAC(self, rightTimeLable.text) = RACObserve(player.bottomToolBar, totalTimeLabel.text);
    
    @weakify(player);
    [RACObserve(self.slider.slider, state) subscribeNext:^(id x) {
        @strongify(player);
        player.bottomToolBar.slider.slider.state = [x integerValue];
    }];
    RAC(self.slider, cache) = RACObserve(player.bottomToolBar.slider, cache);
    [self rac_liftSelector:@selector(setLoading:)
               withSignals:[RACObserve(player.playerManager.playerView.loadingBgView, hidden) not], nil];
}

@end
